﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using MyAspx.Framework.WebUnit.CodeZipTree;
//using WebUI.cs;

namespace WebUI.Services.CodeTree
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Download : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string dir = context.Request["dir"];

            dir = dir.ToOriginalPath();
            if (File.Exists(dir))
            {
                FileStream fs = new FileStream(dir, System.IO.FileMode.Open);
                context.Response.Buffer = false;
                context.Response.AddHeader("Connection", "Keep-Alive");
                context.Response.ContentType = "application/octet-stream";
                context.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(dir));
                context.Response.AddHeader("Content-Length", fs.Length.ToString());

                using (fs)
                {
                    byte[] data = new byte[1024];

                    int byteReads;
                    do
                    {
                        byteReads = fs.Read(data, 0, data.Length);
                        context.Response.BinaryWrite(data);
                    } while (byteReads > 0);

                }

                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
